package com.ibeacon.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ibeacon.controller.common.Log;
import com.ibeacon.controller.common.TokenThread;
import com.ibeacon.model.BeaconInfo;
import com.ibeacon.model.BeaconsInfo;
import com.ibeacon.service.IbeaconDeviceService;


@Controller
public class IbeaconDeviceController {
	
	@Autowired
	IbeaconDeviceService ibeaconDeviceService;
	
	@RequestMapping("/ibeaconDevice")
	public String getIbeaconDevice(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		Log log = Log.getLogger();
		Map<String, String[]>  parameterMap= request.getParameterMap();
		List<HashMap<String, String>> retList = new ArrayList<HashMap<String,String>>();
		for(Map.Entry<String, String[]> map: parameterMap.entrySet()){
			HashMap<String, String> retMap = new HashMap<String, String>();
			 String key = map.getKey();
			 retMap.put("key", key);
			 retMap.put("value", request.getParameter(key));
			 retList.add(retMap);
		}
		model.addAttribute("retList", retList);
		
		String ticket = request.getParameter("ticket");
		log.logger.info("获取ticket："+ticket);
		//如果ticket为空那就要接受xml数据包读取接受到的xml数据包
		StringBuilder xmlsb = new StringBuilder();
		log.logger.info("开始获取xml数据包");
		if(ticket == null ||"".equals(ticket)){
			try {
				BufferedReader buf = new BufferedReader(new InputStreamReader(request.getInputStream()));
				String line = null;
				while((line = buf.readLine())!=null){
					xmlsb.append(line);
					log.logger.info(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
				log.logger.info("获取xml数据失败");
			}
		}
		log.logger.info("获取xml数据包结束");
		/**
		 * 测试ticket
		
		ticket="8a6dede96af871689087ac7d8f9c6ee2";
		 */
//		ticket="2f56f8185f0b3e4ac1d855107aa8172b";
//		xmlsb.append("<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>1433332012</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[ShakearoundUserShake]]></Event><ChosenBeacon><Uuid><![CDATA[uuid]]></Uuid><Major>major</Major><Minor>minor</Minor><Distance>0.057</Distance></ChosenBeacon><AroundBeacons><AroundBeacon><Uuid><![CDATA[uuid]]></Uuid><Major>major</Major><Minor>minor</Minor><Distance>166.816</Distance></AroundBeacon><AroundBeacon><Uuid><![CDATA[uuid]]></Uuid><Major>major</Major><Minor>minor</Minor><Distance>15.013</Distance></AroundBeacon></AroundBeacons></xml>");
		HashMap<String, Object> paraMap = new HashMap<String, Object>();
		if(ticket!=null && !"".equals(ticket)){
			paraMap.put("ticket", ticket);
			List<BeaconInfo> beaconInfo = ibeaconDeviceService.getIbeaconDevice(paraMap);
			model.addAttribute("beaconInfos", beaconInfo);
		}
		
		if(xmlsb.length()!=0){
			BeaconsInfo beaconsInfo =  ibeaconDeviceService.getIbeaconDevices(xmlsb.toString());
			model.addAttribute("beaconsInfo", beaconsInfo);
		}
		model.addAttribute("xmlsb",xmlsb);
		String accessToken = TokenThread.accessToken.getToken();
		System.out.println("accessToken"+accessToken);
		model.addAttribute("accessToken",accessToken);
		
		return "ibeaconDevice";
		
	}
}
